/*
 * KanjiBotFactory.java
 * 
 * Created on Jul 3, 2007, 1:45:24 PM
 * 
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.graphics.bot;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import kanjitori.abstraction.Factory;
import kanjitori.abstraction.Parameterizable;
import kanjitori.util.Cache;
import kanjitori.util.Parameters;

/**
 *
 * @author DGronau
 */
public class KanjiBotFactory implements BotFactory {
    
    private final static String CORPUS_IMAGE = "corpusImage";
    private String corpusImage = "data/corpus.png";
    
    private final static String FONT = "font";
    private Font font = Cache.getFont("Arial Unicode MS" , Font.PLAIN ,200);
    
    private final static String DARK_COLOR = "darkColor";
    private Color darkColor = Cache.getColor(51, 51, 0);
    
    private static final String TEXT_COLOR = "textColor";
    private Color textColor = Color.ORANGE;
    
    private static final String OUTLINE = "outline";
    private boolean outline = true;
    
    private static final String BASELINE = "baseline";
    private int baseline = 0;


    public Bot get(String key) {
        return new KanjiBot(key);
    }

    public void setParams(Map<String, String> map){
        String tempCorpusImage = map.get(CORPUS_IMAGE);
        if (tempCorpusImage != null) { corpusImage = tempCorpusImage; }
        
        Font tempFont = Parameters.getFont(map.get(FONT));
        if (tempFont != null) { font = tempFont; }
        
        Color tempDarkColor = Parameters.getColor(map.get(DARK_COLOR));
        if (tempDarkColor != null) { darkColor = tempDarkColor; }
        
        Color tempTextColor = Parameters.getColor(map.get(TEXT_COLOR));
        if (tempTextColor != null) { textColor = tempTextColor; }
        
        Boolean tempOutline = Parameters.getBoolean(map.get(OUTLINE));
        if (tempOutline != null) { outline = tempOutline; }
        
        Integer tempBaseline = Parameters.getInt(map.get(BASELINE));
        if (tempBaseline != null) { baseline = tempBaseline; }
    }
    
    public Map<String, String> getParams(){
        Map<String, String> map = new HashMap<String, String>();
        map.put(CORPUS_IMAGE, corpusImage);
        map.put(FONT, Cache.fontString(font));
        map.put(DARK_COLOR, Cache.colorString(darkColor));
        map.put(TEXT_COLOR, Cache.colorString(textColor));
        map.put(OUTLINE, "" + outline);
        map.put(BASELINE, "" + baseline);
        return map;
    }
 
    private class KanjiBot extends AbstractBoxBot implements Bot {
    
    /** Creates a new instance of Bot
     * @param value the character which is displayed
     */
    public KanjiBot(String value) {
        super(value);
    }
    
    Image getImage(String character) {
        
        BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        //draw the background texture
        Image corpus = Cache.getImage(corpusImage);
        bg.drawImage(corpus, 0, 0, null);
        //set the colors
        bg.setColor(textColor);
        bg.setFont(font);
        //draw the Kanji background
        bg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        Rectangle2D rec = bg.getFontMetrics().getStringBounds(character, bg);
        int dx = (256 - (int) rec.getWidth()) /2;
        int dy = baseline == 0
                ? (256 - bg.getFontMetrics().getMaxDescent() + bg.getFontMetrics().getMaxAscent()) / 2
                : 256 - baseline;
        bg.drawString(character, dx, dy);
        //draw the Kanji outline
        if (outline) {
            bg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
            bg.setStroke(new BasicStroke(3,
                    BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            bg.setColor(darkColor);
            
            FontRenderContext frc = bg.getFontRenderContext();
            TextLayout tl = new TextLayout(character, font, frc);
            Shape shape = tl.getOutline(AffineTransform.getTranslateInstance(dx, dy));
            bg.draw(shape);
        }
        bg.dispose();
        return bi;
    }
  }
}
